﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.IO;
namespace KinectStudio.ModuliVisione
{
    /// <summary>
    /// Interaction logic for Infrarosso.xaml
    /// </summary>
    
    public partial class Infrarosso : Window
    {

        #region Variabili
            private KinectSensor sensore;
            private WriteableBitmap colorBitmap;
            private byte[] colorPixels;
        #endregion

        public Infrarosso()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (KinectSensor possibileSensore in KinectSensor.KinectSensors)
            {
                if (possibileSensore.Status == KinectStatus.Connected)
                {
                    this.sensore = possibileSensore;
                }
            }
            if (this.sensore != null)
            {
                this.sensore.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);
                this.colorPixels = new byte[this.sensore.ColorStream.FramePixelDataLength];
                this.colorBitmap = new WriteableBitmap(this.sensore.ColorStream.FrameWidth, this.sensore.ColorStream.FrameHeight, 96, 96, PixelFormats.Gray16, null);
                this.ui_anteprima.Source = this.colorBitmap;
                this.sensore.ColorFrameReady += this.SensorColorFrameReady;
                try{
                    this.sensore.Start();
                }catch(IOException){
                    this.sensore = null;
                }
            }
            if (this.sensore == null)
            {
                MessageBox.Show("Nessun Kinect Collegato!", "Errore", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.sensore != null )
            {
                this.sensore.Stop();
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuCliccato = (MenuItem)e.OriginalSource;
            switch (menuCliccato.Header.ToString())
            {
                case "Esci":
                    this.Close();
                    break;
            }
        }
        private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorPixels);

                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        this.colorPixels,
                        this.colorBitmap.PixelWidth * colorFrame.BytesPerPixel,
                        0);
                }
            }
        }
    }
}
